package main

import (
	"fmt"
	"time"
)

// GetWeeksInYear 返回指定年份的所有周（按ISO标准）
func GetWeeksInYear(year int) []struct {
	WeekNum int
	Start   time.Time
	End     time.Time
} {
	// 初始化时间：当年1月1日
	date := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
	var weeks []struct {
		WeekNum int
		Start   time.Time
		End     time.Time
	}

	// 找到第一个周四（ISO标准的第一周必须包含周四）
	for date.Weekday() != time.Thursday {
		date = date.AddDate(0, 0, 1)
	}

	// 循环生成每一周
	for {
		weekNum := getISOWeek(date)
		start := date.AddDate(0, 0, -3) // 本周周一
		end := date.AddDate(0, 0, 3)    // 本周周日

		// 如果跨年，则结束
		if start.Year() > year {
			break
		}

		weeks = append(weeks, struct {
			WeekNum int
			Start   time.Time
			End     time.Time
		}{
			WeekNum: weekNum,
			Start:   start,
			End:     end,
		})

		// 跳到下一周
		date = date.AddDate(0, 0, 7)
	}

	return weeks
}

// getISOWeek 获取ISO周数
func getISOWeek(t time.Time) int {
	_, weekNum := t.ISOWeek()
	return weekNum
}

func main() {
	weeks := GetWeeksInYear(2025)

	fmt.Printf("2025年共有 %d 周（ISO标准）\n", len(weeks))
	for _, week := range weeks {
		fmt.Printf(
			"第 %02d 周: %s 至 %s\n",
			week.WeekNum,
			week.Start.Format("2006-01-02"),
			week.End.Format("2006-01-02"),
		)
	}
}
